package com.investment.advisor.controller;

import com.investment.advisor.dto.PortfolioDTO;
import com.investment.advisor.service.PortfolioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/portfolios")
@Api(tags = "组合管理", description = "组合的增删改查")
public class PortfolioController {

    private final PortfolioService portfolioService;

    @Autowired
    public PortfolioController(PortfolioService portfolioService) {
        this.portfolioService = portfolioService;
    }

    @GetMapping
    @ApiOperation("获取所有组合")
    public ResponseEntity<List<PortfolioDTO>> getAllPortfolios() {
        return ResponseEntity.ok(portfolioService.getAllPortfolios());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取组合")
    public ResponseEntity<PortfolioDTO> getPortfolioById(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long id) {
        return ResponseEntity.ok(portfolioService.getPortfolioById(id));
    }

    @GetMapping("/search")
    @ApiOperation("搜索组合")
    public ResponseEntity<List<PortfolioDTO>> searchPortfolios(
            @ApiParam(value = "搜索关键词", required = true)
            @RequestParam String keyword) {
        return ResponseEntity.ok(portfolioService.searchPortfolios(keyword));
    }

    @GetMapping("/strategy/{strategyId}")
    @ApiOperation("根据策略ID获取组合")
    public ResponseEntity<List<PortfolioDTO>> getPortfoliosByStrategyId(
            @ApiParam(value = "策略ID", required = true)
            @PathVariable Long strategyId) {
        return ResponseEntity.ok(portfolioService.getPortfoliosByStrategyId(strategyId));
    }

    @GetMapping("/fund/{fundId}")
    @ApiOperation("根据基金ID获取组合")
    public ResponseEntity<List<PortfolioDTO>> getPortfoliosByFundId(
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId) {
        return ResponseEntity.ok(portfolioService.getPortfoliosByFundId(fundId));
    }

    @GetMapping("/creator/{creator}")
    @ApiOperation("根据创建者获取组合")
    public ResponseEntity<List<PortfolioDTO>> getPortfoliosByCreator(
            @ApiParam(value = "创建者", required = true)
            @PathVariable String creator) {
        return ResponseEntity.ok(portfolioService.getPortfoliosByCreator(creator));
    }

    @PostMapping
    @ApiOperation("创建组合")
    public ResponseEntity<PortfolioDTO> createPortfolio(
            @ApiParam(value = "组合信息", required = true)
            @RequestBody PortfolioDTO portfolioDTO) {
        return new ResponseEntity<>(portfolioService.createPortfolio(portfolioDTO), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新组合")
    public ResponseEntity<PortfolioDTO> updatePortfolio(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "组合信息", required = true)
            @RequestBody PortfolioDTO portfolioDTO) {
        return ResponseEntity.ok(portfolioService.updatePortfolio(id, portfolioDTO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除组合")
    public ResponseEntity<Void> deletePortfolio(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long id) {
        portfolioService.deletePortfolio(id);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/{id}/funds")
    @ApiOperation("更新组合的基金配置")
    public ResponseEntity<PortfolioDTO> updatePortfolioFunds(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "基金权重配置", required = true)
            @RequestBody Map<Long, BigDecimal> fundWeights) {
        return ResponseEntity.ok(portfolioService.updatePortfolioFunds(id, fundWeights));
    }

    @PostMapping("/{portfolioId}/funds/{fundId}")
    @ApiOperation("向组合添加基金")
    public ResponseEntity<PortfolioDTO> addFundToPortfolio(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long portfolioId,
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId,
            @ApiParam(value = "权重", required = true)
            @RequestParam BigDecimal weight) {
        return ResponseEntity.ok(portfolioService.addFundToPortfolio(portfolioId, fundId, weight));
    }

    @DeleteMapping("/{portfolioId}/funds/{fundId}")
    @ApiOperation("从组合移除基金")
    public ResponseEntity<PortfolioDTO> removeFundFromPortfolio(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long portfolioId,
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId) {
        return ResponseEntity.ok(portfolioService.removeFundFromPortfolio(portfolioId, fundId));
    }

    @PutMapping("/{portfolioId}/funds/{fundId}/weight")
    @ApiOperation("更新组合中基金的权重")
    public ResponseEntity<PortfolioDTO> updateFundWeight(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long portfolioId,
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId,
            @ApiParam(value = "权重", required = true)
            @RequestParam BigDecimal weight) {
        return ResponseEntity.ok(portfolioService.updateFundWeight(portfolioId, fundId, weight));
    }
} 